package com.tusuo.stds.ui.vip;

import android.content.Intent;
import android.support.annotation.NonNull;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hannesdorfmann.mosby.mvp.MvpPresenter;
import com.hannesdorfmann.mosby.mvp.MvpView;
import com.tusuo.stds.MyApp;
import com.tusuo.stds.R;
import com.tusuo.stds.di.DaggerScope;
import com.tusuo.stds.di.Injector;
import com.tusuo.stds.rest.ApiHelper;
import com.tusuo.stds.rest.UserInfo;
import com.tusuo.stds.rest.VipList;
import com.tusuo.stds.ui.BaseToolbarActivity;
import com.tusuo.stds.ui.ToolbarOwner;
import com.tusuo.stds.ui.complete.CompleteQuestionActivity_;
import com.tusuo.stds.ui.integrity.IntegrityDetailActivity;
import com.tusuo.stds.ui.pay.MyPayActivity_;
import com.tusuo.stds.util.Utils;

import org.androidannotations.annotations.AfterInject;
import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;

import java.util.ArrayList;
import java.util.Locale;

import javax.inject.Inject;

import autodagger.AutoComponent;
import autodagger.AutoInjector;
import rx.Subscriber;

/**
 * Created by developer on 17-9-14.
 */
@AutoComponent(
        dependencies = MyApp.class
)
@AutoInjector
@DaggerScope(PrivilegeActivity.class)
@EActivity(R.layout.activity_privilege)
public class PrivilegeActivity extends BaseToolbarActivity {

    @Inject
    ApiHelper mApiHelper;

    @ViewById(R.id.privilege_include_pay)
    LinearLayout layoutPay;
    @ViewById(R.id.vip_layout_price_group)
    LinearLayout layoutGroup;
    @ViewById(R.id.privilege_layout_hint)
    RelativeLayout layoutHint;
    @ViewById(R.id.privilege_btn_pay)
    Button btnPay;

    private UserInfo mUserInfo;
    private ArrayList<VipList.VipItem> mVipData;
    private TranslateAnimation animation_show;
    private TranslateAnimation animation_hide;
    private boolean isShowAnimationFinish = true;
    private boolean isHideAnimationFinish = true;

    @AfterInject
    void initDI() {
        Injector.initPrivilegeActivityComponent().inject(this);
    }

    @AfterViews
    void initData() {
        setupUI(this,getResources().getColor(R.color.white),getResources().getColor(R.color.black));
        ToolbarOwner.Config config = new ToolbarOwner.Config("特权详情", true, R.drawable.ic_back_dark, null);
        mToolbarOwner.setConfig(config);
        initAnimation();
        mApiHelper.getUserInfo(new GetUserInfoSubscriber());
    }

    void initAnimation() {
        animation_show = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, 0,
                Animation.RELATIVE_TO_PARENT, 0,
                Animation.RELATIVE_TO_PARENT, 1,
                Animation.RELATIVE_TO_PARENT, 0);

        animation_show.setDuration(500);
        animation_show.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
                isShowAnimationFinish = false;
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                isShowAnimationFinish = true;
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });


        animation_hide = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, 0,
                Animation.RELATIVE_TO_PARENT, 0,
                Animation.RELATIVE_TO_PARENT, 0,
                Animation.RELATIVE_TO_PARENT, 1);

        animation_hide.setDuration(500);
        animation_hide.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
                isHideAnimationFinish = false;
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                isHideAnimationFinish = true;
                layoutPay.setVisibility(View.GONE);
                layoutHint.setVisibility(View.GONE);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });

    }


    private void initVip() {

        for (final VipList.VipItem item : mVipData) {
            View view = View.inflate(this,R.layout.item_vip_price,null);
            TextView tvMonth = (TextView) view.findViewById(R.id.item_vip_tv_month);
            TextView tvPrice = (TextView) view.findViewById(R.id.item_vip_tv_price);
            tvMonth.setText(item.Title);
            tvPrice.setText(String.format(Locale.SIMPLIFIED_CHINESE, "%.0f", item.Price));

            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent();
                    intent.setClass(PrivilegeActivity.this, MyPayActivity_.class);
                    intent.putExtra("payType","vip");
                    intent.putExtra("vipId",item.Id);
                    intent.putExtra("vipMonth",item.Title);
                    intent.putExtra("vipPrice",item.Price);
                    startActivity(intent);
                }
            });

            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT,1.0f);
            int px = Utils.dp2px(15, this);
            params.setMargins(0,0,px,0);

            layoutGroup.addView(view,params);
        }

    }

    private void initButton() {
        if (mUserInfo.VipLevel == 0) {
            // 不是VIP
            btnPay.setText("开通会员");
        } else {
            // 是VIP
            btnPay.setText("续费会员");
        }
        btnPay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 弹出vip付费
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        showPayLayout();
                    }
                });
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (layoutPay != null) {
            layoutPay.setVisibility(View.INVISIBLE);
        }

        if (layoutPay != null && layoutHint != null) {
            layoutPay.setVisibility(View.GONE);
            layoutHint.setVisibility(View.GONE);

        }

        if (mUserInfo != null) {
            mApiHelper.getUserInfo(new GetUserInfoSubscriber());
        }

    }

    void showPayLayout() {
        if (layoutPay.getVisibility() != View.VISIBLE && isShowAnimationFinish && isHideAnimationFinish) {
            layoutPay.setVisibility(View.VISIBLE);
            layoutHint.setVisibility(View.VISIBLE);
            layoutPay.startAnimation(animation_show);
        }
    }

    @Click({R.id.vip_layout_iv_close,R.id.privilege_layout_hint})
    void hidePayLayout() {
        if (layoutPay.getVisibility() == View.VISIBLE && isShowAnimationFinish && isHideAnimationFinish) {
            layoutPay.startAnimation(animation_hide);
        }
    }


    @NonNull
    @Override
    public MvpPresenter createPresenter() {
        return new MvpPresenter() {
            @Override
            public void attachView(MvpView view) {
            }
            @Override
            public void detachView(boolean retainInstance) {
            }
        };
    }



    private class GetUserInfoSubscriber extends Subscriber<UserInfo> {

        @Override
        public void onCompleted() {

        }

        @Override
        public void onError(Throwable e) {

        }

        @Override
        public void onNext(UserInfo userInfo) {
            if (userInfo != null) {
                mUserInfo = userInfo;
                initButton();
                if (mVipData == null) {
                    mApiHelper.getVipList(new GetVipListSubscriber());
                }
            }
        }
    }

    private class GetVipListSubscriber extends Subscriber<VipList> {

        @Override
        public void onCompleted() {

        }

        @Override
        public void onError(Throwable e) {

        }

        @Override
        public void onNext(VipList vipList) {
            if (vipList.Data != null && mVipData == null) {
                mVipData = vipList.Data;
                initVip();
            }
        }
    }



}
